package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.PatternGoods;
import cn.signalpha.modules.shop.mapper.PatternGoodsMapper;
import cn.signalpha.modules.shop.vo.PatternGoodsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class PatternGoodsService extends ServiceImpl<PatternGoodsMapper, PatternGoods> {

	/**
	 * 自定义分页
	 */
    public IPage<PatternGoodsVO> selectPatternGoodsPage(IPage<PatternGoodsVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectPatternGoodsPage(page, params));
    }

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<PatternGoods> selectPatternGoodsWrapper(Map<String, Object> params) {
        QueryWrapper<PatternGoods> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("id")), PatternGoods::getId, params.get("id"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("goodsId")), PatternGoods::getGoodsId, params.get("goodsId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("patternId")), PatternGoods::getPatternId, params.get("patternId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("patternType")), PatternGoods::getPatternType, params.get("patternType"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("config")), PatternGoods::getConfig, params.get("config"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("sort")), PatternGoods::getSort, params.get("sort"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("createTime")), PatternGoods::getCreateTime, params.get("createTime"));
        wrapper.lambda().orderByDesc(PatternGoods::getId);
        return wrapper;
    }

	/**
	 * 根据商品id删除
	 */
	public void removeByGoodsId(Long goodsId) {
		baseMapper.delete(Wrappers.<PatternGoods>lambdaQuery().eq(PatternGoods::getGoodsId, goodsId));
	}

	/**
	 * 是否已添加
	 */
	public boolean isExists(Long patternId, Long goodsId) {
		return baseMapper.selectCount(Wrappers.<PatternGoods>lambdaQuery().eq(PatternGoods::getPatternId, patternId).eq(PatternGoods::getGoodsId, goodsId)) > 0;
	}

}

